<template>

    <UxDialog :visible="value" @close="close()" class="branding-dialog" title="Custom neck label">

        <!-- 领标编辑 -->
        <div class="branding-editor" v-if="value">
            <!-- 设计区 -->
            <div class="layout-render" ref="warper">
                <div class="render-viewbox" :style="size" v-if="size">
                    <!-- <div class="render-mask" v-if="value.mask_image">
                        <img :src="$getImageUrl(value.mask_image)" crossOrigin="anonymous">
                    </div> -->

                    <!-- 效果区 -->
                    <UserBrandingRender :piece="value" ref="render" />
                    <!-- 操作区 -->
                    <DesignerEditorOperate :activeSurface="surface" :activePiece="value" :activeLayer="active" @selectLayer="selectLayer($event)" @updateLayer="updateLayer($event)" />
                </div>
            </div>

            <div class="layout-align">

                <div class="layout-align-warp">
                    <h2 v-if="theme == 'dark'">Neck label for dark-colored clothes</h2>
                    <h2 v-if="theme == 'light'">Neck label for light-colored clothes</h2>

                    <div class="branding-align-image" :class="{active:active == imageLayer}">

                        <h3>
                            <figure>
                                <img crossOrigin="anonymous" :style="$fiexImage(imageLayer.w,imageLayer.h,1)" v-lazy="$getImageUrl(imageLayer.url,'mini')" />
                            </figure>
                            <strong v-text="imageLayer.name"></strong>
                        </h3>

                        <div class="factor-align">
                            <button @click="setAlign('image', 'left')">
                                <svg-icon name="tooler-left" />
                            </button>
                            <button @click="setAlign('image', 'center')">
                                <svg-icon name="tooler-center" />
                            </button>
                            <button @click="setAlign('image', 'right')">
                                <svg-icon name="tooler-right" />
                            </button>
                            <button @click="setAlign('image', 'top')">
                                <svg-icon name="tooler-top" />
                            </button>
                            <button @click="setAlign('image', 'middle')">
                                <svg-icon name="tooler-middle" />
                            </button>
                            <button @click="setAlign('image', 'bottom')">
                                <svg-icon name="tooler-bottom" />
                            </button>
                        </div>
                    </div>

                    <div class="branding-align-size" :class="{active:active == textLayer}">
                        <h3>Size</h3>

                        <div class="factor-size">
                            <button :class="{active: textLayer.text == 'XS' }" @click="setText('XS')">XS</button>
                            <button :class="{active: textLayer.text == 'S' }" @click="setText('S')">S</button>
                            <button :class="{active: textLayer.text == 'M' }" @click="setText('M')">M</button>
                            <button :class="{active: textLayer.text == 'L' }" @click="setText('L')">L</button>
                            <button :class="{active: textLayer.text == 'XL' }" @click="setText('XL')">XL</button>
                            <button :class="{active: textLayer.text == '2XL' }" @click="setText('2XL')">2XL</button>
                            <button :class="{active: textLayer.text == '3XL' }" @click="setText('3XL')">3XL</button>
                            <button :class="{active: textLayer.text == '4XL' }" @click="setText('4XL')">4XL</button>
                            <button :class="{active: textLayer.text == '5XL' }" @click="setText('5XL')">5XL</button>
                        </div>
                        <div class="factor-color">
                            <UxColorPicker name="Color" v-model="fontColor" />
                            <UxInputNumber name="Font size" unit="%" v-model="layerFontSize" :min="1" :max="1000" :step="0.1" :precision="2" />
                        </div>

                        <div class="factor-align">
                            <button @click="setAlign('size', 'left')">
                                <svg-icon name="tooler-left" />
                            </button>
                            <button @click="setAlign('size', 'center')">
                                <svg-icon name="tooler-center" />
                            </button>
                            <button @click="setAlign('size', 'right')">
                                <svg-icon name="tooler-right" />
                            </button>
                            <button @click="setAlign('size', 'top')">
                                <svg-icon name="tooler-top" />
                            </button>
                            <button @click="setAlign('size', 'middle')">
                                <svg-icon name="tooler-middle" />
                            </button>
                            <button @click="setAlign('size', 'bottom')">
                                <svg-icon name="tooler-bottom" />
                            </button>
                        </div>

                        <p><i class="iconfont icon-xiangqing1"></i>The letter size printed out will depend on the actual size of the garment you order.</p>

                    </div>

                </div>

                <div class="branding-save">
                    <button @click="beforeClose()">Cancel</button>
                    <button @click="saveDesign()" type="primary">Save</button>
                </div>

            </div>
        </div>

    </UxDialog>

</template>
<script>
import DesignUtils from "@/utils/DesignUtils.js";
export default {
    name: "UserBrandingEditor",
    props: {
        value: { type: Object, default () { return {} } },
        theme: { type: String, default: '' }
    },
    data () {
        return {
            active: null,
            change: false,
            size: null,
        };
    },
    computed: {
        surface () {
            const piece = this.value || {};
            return {
                outer_width: piece.outer_width,
                outer_height: piece.outer_height,
                pieces: [piece],
            }
        },

        imageLayer () {
            const piece = this.value || {};
            const layers = piece.layers || [];
            return layers[0] || {}
        },

        textLayer () {
            const piece = this.value || {};
            const layers = piece.layers || [];
            return layers[1] || {}
        },

        fontColor: {
            get () {
                const piece = this.value || {};
                const layers = piece.layers || [];
                const text = layers[1] || {}
                return text.font_color;
            },
            set (v) {
                const piece = this.value || {};
                const layers = piece.layers || [];
                const text = layers[1] || {}
                text.font_color = v;
            }
        },
        layerFontSize: {
            get () {
                const piece = this.value || {};
                const layers = piece.layers || [];
                const text = layers[1] || {}
                const w = piece.canvas_width || 1;
                const h = piece.canvas_height || 1;
                return 100 * text.font_size / Math.min(w, h);
            },
            set (value) {

                const piece = this.value || {};
                const layers = piece.layers || [];
                const text = layers[1] || {}
                const w = piece.canvas_width || 1;
                const h = piece.canvas_height || 1;

                const format = 0.01 * value * Math.min(w, h);

                if (text.font_size != format) {

                    const style = {
                        textAlign: 'center',
                        fontFamily: text.font_family,
                        fontSize: format,
                        lineHeight: format,
                        charSpacing: text.letter_spacing,
                    }
                    const size = DesignUtils.getTextSize(text.text, style);
                    text.x = text.x + (text.w - size.width) / 2;
                    text.y = text.y + (text.h - size.height) / 2;
                    text.w = size.width;
                    text.h = size.height;
                    text.font_size = format;
                }
            }
        },
    },
    watch: {
        value (newval, oldval) {
            if (newval) {
                this.change = false;
                this.active = null;
                this.loading = false;
                this.$nextTick(e => this.resize())
            }
        }
    },
    methods: {

        // 选中图层
        selectLayer (layer) {
            this.active = layer;
        },
        // 更新图层信息
        updateLayer (change) {
            this.active && Object.keys(change).map(key => {
                this.active[key] = change[key]
            })
            this.change = true;
        },

        // 对齐
        setAlign (type, align) {

            const piece = this.value || {};
            const layers = piece.layers || [];
            const layer = type == 'image' ? layers[0] : layers[1];

            if (align == 'left') {
                // 水平居左
                layer.x = 0;
            } else if (align == 'center') {
                // 水平居中
                layer.x = 0.5 * (piece.canvas_width - layer.w);
            } else if (align == 'right') {
                // 水平居右
                layer.x = piece.canvas_width - layer.w;
            } else if (align == 'top') {
                // 垂直居上
                layer.y = 0;
            } else if (align == 'middle') {
                // 垂直居中
                layer.y = 0.5 * (piece.canvas_height - layer.h);
            } else if (align == 'bottom') {
                // 垂直居下
                layer.y = piece.canvas_height - layer.h;
            }
        },

        setText (text) {

            const piece = this.value || {};
            const layers = piece.layers || [];
            const layer = layers[1];
            const style = {
                textAlign: 'center',
                fontFamily: layer.font_family,
                fontSize: layer.font_size,
                lineHeight: layer.font_size,
                charSpacing: layer.letter_spacing,
            }
            const size = DesignUtils.getTextSize(text, style);
            layer.text = text;
            layer.x = layer.x + (layer.w - size.width) / 2;
            layer.y = layer.y + (layer.h - size.height) / 2;
            layer.w = size.width;
            layer.h = size.height;

        },

        // 保存
        saveDesign () {
            const piece = JSON.parse(JSON.stringify(this.value));
            const request = {
                spu: 'necklabel',
                pieces: [piece],
                models: [],
            }

            // 替换为L码保存
            // piece.layers[1].text = 'L';

            this.$parent.dialog_saving = true;
            this.$http("Designer.batchDraw", request).then((response) => {
                if (response.code === 0) {
                    const output = (response.data[0] || {}).output || ''
                    this.value.output_img = output.replace(/-litimg(\.(jpe?g|png))/gi, "$1");
                    this.$parent.dialog_saving = false;
                    this.$parent.dialog_success = () => {
                        this.$parent.dialog_success = null;
                        this.$emit('success', this.value);
                        this.$emit('input', null);
                    }

                } else {
                    this.$parent.dialog_saving = false;
                    this.$parent.dialog_failure = () => {
                        this.$parent.dialog_failure = null;
                        this.saveDesign();
                    }

                }
            })
        },

        // 关闭
        close () {
            this.$emit("input", null);
        },

        beforeClose () {
            if (this.change) {
                this.$emit('beforeClose', () => {
                    this.close();
                })
            } else {
                this.close();
            }
        },

        resize () {

            if (this.$refs.warper) {
                const rect = this.$refs.warper.getBoundingClientRect();
                const min = Math.min(rect.width, rect.height);
                this.size = { width: 100 * min / rect.width + '%', height: 100 * min / rect.height + '%' }
                this.$refs.render && this.$refs.render.resize && this.$refs.render.resize()
            }

        }
    },

    created () {
        window.addEventListener('resize', this.resize, false);
        window.onbeforeunload = (event) => {
            let notify = "Changes that you made may not be saved.";
            if (!this.change) {
            } else {
                (event || window.event).returnValue = notify;
                return notify;
            }
        };
    },
    beforeDestroy () {
        window.removeEventListener('resize', this.init, false);
    }

};
</script>
<style lang="scss" scoped>
.branding-dialog {
    padding: 0 !important;

    &::v-deep > .dialog-content {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;

        & > h2 {
            height: 78px;
            line-height: 78px;
            font-size: 24px;
            font-weight: bold;
            color: #000000;
        }

        & > h6 {
            display: none;
        }
    }
}
.branding-editor {
    width: 100%;
    height: 100%;
    background-color: #fff;
    box-sizing: border-box;
    z-index: 99;
    display: flex;
}

.layout-render {
    flex: 1382;
    position: relative;
    background: #eee;
    overflow: hidden;
    max-height: 100%;

    .render-viewbox {
        position: absolute;
        left: 50%;
        top: 50%;
        height: 100%;
        transform: translate(-50%, -50%);

        &::v-deep .editor-operate-drager button:nth-child(4) {
            display: none;
        }
    }

    .render-mask {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;

        img {
            width: 100%;
            height: 100%;
        }
    }
}

.layout-align {
    width: 538px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

.layout-align-warp {
    padding: 0 60px 60px;
    flex: 1;
    overflow: auto;

    h2 {
        font-size: 22px;
        color: #000000;
        line-height: 24px;
        padding: 32px 0;
        text-align: center;
    }

    .branding-align-image {
        background: #ffffff;
        border-radius: 6px;
        border: 1px solid #c6c6c6;
        overflow: hidden;

        &.active {
            border-color: #ea4c89;
        }

        h3 {
            display: flex;
            height: 60px;
            background: #f6f6f6;
            border-bottom: 1px solid #ededed;
            white-space: nowrap;
            figure {
                width: 40px;
                min-width: 40px;
                height: 40px;
                margin: 10px;
                position: relative;
                border-radius: 2px;
                overflow: hidden;
                &::after {
                    content: '';
                    position: absolute;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    top: 0;
                    border: 1px solid #ddd;
                    border-radius: 2px;
                }
                img {
                    position: absolute;
                }
            }
            strong {
                flex: 1;
                overflow: hidden;
                text-overflow: ellipsis;
                line-height: 60px;
                margin-left: 2px;
                font-size: 14px;
                color: #000000;
            }
        }
    }

    .branding-align-size {
        overflow: hidden;
        margin-top: 40px;
        background: #ffffff;
        border-radius: 6px;
        border: 1px solid #c6c6c6;
        padding-bottom: 26px;
        &.active {
            border-color: #ea4c89;
        }

        h3 {
            height: 40px;
            line-height: 40px;
            background: #f6f6f6;
            border-bottom: 1px solid #ededed;
            text-align: center;
            font-size: 14px;
            color: #000000;
        }

        & > p {
            margin: 0 38px;
            background: #f6f6f6;
            border-radius: 6px;
            position: relative;
            font-size: 12px;
            color: #000000;
            line-height: 18px;
            padding: 7px 12px 7px 28px;

            i {
                position: absolute;
                width: 12px;
                height: 18px;
                line-height: 18px;
                font-size: 12px;
                left: 12px;
                top: 7px;
                color: #acacac;
            }
        }
    }
}

.branding-save {
    display: flex;
    position: relative;
    height: 44px;
    padding: 30px 60px 32px;

    &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 1px;
        background-color: #dcdcdc;
    }

    button {
        flex: 140;

        &[type='primary'] {
            flex: 262;
        }

        & + button {
            margin-left: 16px;
        }
    }
}

.factor-align {
    padding: 24px 38px;
    display: flex;
    justify-content: space-between;
    button {
        margin: 5px 0;
        width: 24px;
        height: 24px;
        line-height: 24px;
        text-align: center;
        background: none;
        border: none;
        font-size: 22px;
        color: #666;
        position: relative;

        &:nth-child(3) {
            margin-right: 20px;
        }
        svg {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 24px;
            height: 24px;

            stroke: #555;
            fill: #555;
        }

        &:hover {
            svg {
                stroke: #ea4c89;
                fill: #ea4c89;
            }
        }
    }
}

.factor-size {
    padding: 20px 34px;
    display: flex;
    flex-wrap: wrap;
    button {
        width: 42px;
        height: 32px;
        margin: 4px;
        line-height: 30px;
        text-align: center;
        font-size: 12px;
        color: #666;

        &.active {
            color: #ea4c89;
            border-color: #ea4c89;
        }
    }
}

.factor-color {
    margin: 0 38px 0;
    border-top: 1px solid #e0e0e0;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    padding: 24px 0;

    .input-number {
        width: 120px;
        margin-left: 42px;
    }
}

.dialog-saving::v-deep .dialog-content {
    width: 400px;
    height: 280px;
    background: #fafafa;
    border-radius: 38px;

    .dialog-body {
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 60px;

        figure {
            width: 66px;
            height: 66px;
            margin-bottom: 24px;
            img {
                width: 100%;
                height: 100%;
            }
        }

        & > img {
            height: 8px;
        }

        strong {
            margin-top: 24px;
            font-size: 21px;
            color: #000000;
            line-height: 32px;
        }
    }

    h6 {
        display: none;
    }
}

.dialog-success,
.dialog-failure {
    &::v-deep .dialog-content {
        width: 320px;
        height: 414px;

        .dialog-body {
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: center;
            padding: 40px 20px 26px;
        }

        figure {
            margin-top: 50px;
            width: 80px;
            height: 80px;

            img {
                width: 100%;
                height: 100%;
            }
        }

        p {
            height: 120px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            font-size: 16px;
            color: #000000;
            line-height: 20px;
            text-align: center;
            strong {
                color: #000000;
                margin-bottom: 8px;
            }
        }

        fieldset {
            display: flex;
            flex-direction: column;
            width: 100%;
            button + button {
                margin-top: 8px;
            }
        }
    }
}
</style>
